﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using yncTech.BusinessObjects;
using yncTech.Common;
using EntitySpaces.Interfaces;

public partial class Admin_Fin_Fin002b : AdminBasePage
{
    decimal boxID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[VBoxReceiptsMetadata.ColumnNames.BoxID] != null)
        {
            boxID = decimal.Parse(Request.QueryString[VBoxReceiptsMetadata.ColumnNames.BoxID]);
        }

        if (!IsPostBack)
        {
            drpYear.Items.Add(new ListItem(DateTime.Now.AddYears(-1).Year.ToString(), DateTime.Now.AddYears(-1).Year.ToString()));
            drpYear.Items.Add(new ListItem(DateTime.Now.Year.ToString(), DateTime.Now.Year.ToString()));
            drpYear.SelectedValue = DateTime.Now.Year.ToString();

            drpYear.Enabled = false;
            FinBox box = new FinBox();
            box.LoadByPrimaryKey(boxID);
            drpYear.SelectedValue = box.BoxYear.ToString();
            drpDocType.SelectedValue = box.DocumentType;
            drpLocation.SelectedValue = box.BoxLocation.ToString();
            drpCompany.SelectedValue = box.CompanyCode;

            if (box.StorageStatus == ConstantValue.StorageStatusCode.PendingTransfer || box.StorageStatus == ConstantValue.StorageStatusCode.Transfer)
            {
                btnRemove.Visible = false;
                btnRemove.Enabled = false;
            }
        }

    }

    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }
        GridDataBind(e.NewPageIndex);
    }

    void GridDataBind(int page)
    {

        FinReceiptQuery q1 = new FinReceiptQuery("q1");
        FinBoxReceiptsQuery q2 = new FinBoxReceiptsQuery("q2");

        q1.InnerJoin(q2).On(q1.ReceiptID == q2.ReceiptID);
        q1.SelectAll();

        q1.Where(q2.BoxID == boxID);
        pageGrid.DataSource = q1.LoadDataTable();
        pageGrid.CurrentPage = page;
        pageGrid.DataBind();
    }


    protected void btnRemove_Click(object sender, EventArgs e)
    {
        try
        {
            lbMessage.Text = "";
          
            List<decimal> receipts = new List<decimal>();
            using (esTransactionScope tr = new esTransactionScope())
            {
                foreach (RepeaterItem item in pageGrid.Items)
                {
                    CheckBox chkBox = (CheckBox)item.FindControl("chk_sel");
                    if (chkBox.Checked)
                    {
                        Label lbReceipt = (Label)item.FindControl("lbReceiptID");
                        receipts.Add(decimal.Parse(lbReceipt.Text));
                    }
                }

                if (receipts.Count > 0)
                {
                    FinBoxReceiptsCollection selectedReceipts = new FinBoxReceiptsCollection();
                    selectedReceipts.Query.Where(selectedReceipts.Query.ReceiptID.In(receipts.ToArray()));
                    selectedReceipts.Query.Load();
                    selectedReceipts.MarkAllAsDeleted();
                    selectedReceipts.Save();

                    FinReceiptCollection finReceipts = new FinReceiptCollection();
                    finReceipts.Query.Where(finReceipts.Query.ReceiptID.In(receipts.ToArray()));
                    finReceipts.Query.Load();

                    foreach (var item in finReceipts)
                    {
                        item.BoxID = -1;
                        item.UpdatedDttm = DateTime.Now;
                        item.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                    }
                    finReceipts.Save();
                    FinBox box = new FinBox();
                    box.LoadByPrimaryKey(boxID);

                    box.BoxNo = GetBoxNo(boxID);
                    box.Save();

                }

                tr.Complete();

                WebHelper.CloseDialog(true);
            }

        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }

}